{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 1,
   "id": "8715031f",
   "metadata": {},
   "outputs": [],
   "source": [
    "def begin_train_myFinetuneModel(num,model):\n",
    "    optimizer = optim.SGD(model.parameters(), lr=lr_preSet, weight_decay=weight_decay_preSet)\n",
    "    for epoch in range(num):\n",
    "        num1=0\n",
    "        print(\"当前轮数：\",epoch)\n",
    "        for step, batch_data in enumerate(train_loader):\n",
    "            num1=num1+1\n",
    "            # 1. 清空梯度\n",
    "            b_ids = batch_data['ids'].to(device)\n",
    "            b_labels=batch_data['labels'].to(device)\n",
    "            b_speaker = batch_data['speaker']\n",
    "            model.zero_grad()\n",
    "            # 2. 运行模型\n",
    "            speakers = []\n",
    "            b_speaker = batch_data['speaker']\n",
    "            for i in b_speaker:\n",
    "                    i = list(i)[0]\n",
    "                    if i =='客服':\n",
    "                        speakers.append(-100)\n",
    "                    else:\n",
    "                        speakers.append(-200)\n",
    "            speakers = torch.tensor(speakers)\n",
    "            speakers = speakers.unsqueeze(1).unsqueeze(0).to(device)\n",
    "            loss = model(b_ids[0], b_labels,speakers) \n",
    "            if (num1==100):\n",
    "                print(loss.item(),epoch)\n",
    "                num1=0\n",
    "            # 3. 计算loss值，梯度并更新权重参数                                 \n",
    "            loss.backward()    #retain_graph=True)  #反向传播，计算当前梯度\n",
    "            optimizer.step()  #根据梯度更新网络参数\n",
    "            torch.cuda.empty_cache()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "id": "9e5c198f",
   "metadata": {},
   "outputs": [],
   "source": [
    "def begin_train_myImportantModel(num,model):\n",
    "    criterion = torch.nn.BCELoss(reduction='mean')\n",
    "    optimizer = optim.SGD(model.parameters(), lr=lr_preSet, weight_decay=weight_decay_preSet)\n",
    "    for epoch in range(num):\n",
    "        num1=0\n",
    "        print(\"当前轮数：\",epoch)\n",
    "        for step, batch_data in enumerate(train_loader):\n",
    "            num1=num1+1\n",
    "            # 1. 清空梯度\n",
    "            b_ids = batch_data['ids'].to(device)\n",
    "            b_important=batch_data['importants'].to(device)\n",
    "            model.zero_grad()\n",
    "            # 2. 运行模型\n",
    "            pre = model(b_ids[0]).squeeze(-1).to(device)\n",
    "            print(pre)\n",
    "            loss = criterion(pre, b_important)\n",
    "            print(loss.item(),epoch)\n",
    "            # 3. 计算loss值，梯度并更新权重参数                                 \n",
    "            loss.backward()    #retain_graph=True)  #反向传播，计算当前梯度\n",
    "            optimizer.step()  #根据梯度更新网络参数\n",
    "            torch.cuda.empty_cache()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "773ebf4a",
   "metadata": {},
   "outputs": [],
   "source": [
    "def begin_test_myImportantModel(model,loader,num):\n",
    "    for epoch in range(1):\n",
    "        num1=0\n",
    "        print(\"当前轮数：\",epoch)\n",
    "        arr_important_index = []\n",
    "        arr_intent = []\n",
    "        for step, batch_data in enumerate(loader):\n",
    "            num1=num1+1\n",
    "            # 1. 清空梯度\n",
    "            b_ids = batch_data['ids'].to(device)\n",
    "            b_intent = batch_data['intent']\n",
    "            model.zero_grad()\n",
    "            # 2. 运行模型\n",
    "            model.eval()\n",
    "            with torch.no_grad():\n",
    "                arr = []\n",
    "                arr1 = []\n",
    "                pre = model(b_ids[0]).squeeze(-1).to(device)\n",
    "                pre = pre.squeeze(0)\n",
    "                k=0\n",
    "                while(k<num):\n",
    "                    max = pre.argmax()\n",
    "                    if batch_data['speaker'][max][0]=='客服':\n",
    "                        pre[max]=-100.0\n",
    "                    else:\n",
    "                        arr.append(max)\n",
    "                        arr1.append(b_intent[max])\n",
    "                        k+=1\n",
    "                        pre[max]=-100.0\n",
    "                arr_important_index.append(arr)\n",
    "                arr_intent.append(arr1)\n",
    "        return arr_important_index,arr_intent\n",
    "        "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "id": "68390563",
   "metadata": {},
   "outputs": [],
   "source": [
    "def begin_train_myDomainModel(num,model):\n",
    "    criterion = torch.nn.CrossEntropyLoss()\n",
    "    optimizer = optim.SGD(model.parameters(), lr=lr_preSet, weight_decay=weight_decay_preSet)\n",
    "    for epoch in range(num):\n",
    "        num1=0\n",
    "        print(\"当前轮数：\",epoch)\n",
    "        for step, batch_data in enumerate(domain_train_loader):\n",
    "            num1=num1+1\n",
    "            # 1. 清空梯度\n",
    "            b_ids = batch_data['ids'].to(device)\n",
    "            b_domain=batch_data['domains'].to(device)\n",
    "            model.zero_grad()\n",
    "            # 2. 运行模型\n",
    "            pre = model(b_ids).squeeze(0)\n",
    "            loss = criterion(pre, b_domain)\n",
    "            if (num1==5):\n",
    "                print(pre,b_domain)\n",
    "                print(loss.item(),epoch)\n",
    "                num1=0\n",
    "            # 3. 计算loss值，梯度并更新权重参数                                 \n",
    "            loss.backward()    #retain_graph=True)  #反向传播，计算当前梯度\n",
    "            optimizer.step()  #根据梯度更新网络参数\n",
    "            torch.cuda.empty_cache()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "0a38d00d",
   "metadata": {},
   "outputs": [],
   "source": [
    "def begin_test_myDomainModel(model,add_important,loader):\n",
    "    for epoch in range(1):\n",
    "        num1=0\n",
    "        print(\"当前轮数：\",epoch)\n",
    "        arr=[]\n",
    "        for step, batch_data in enumerate(loader):\n",
    "            # 1. 清空梯度\n",
    "            b_ids = batch_data['ids'].to(device)\n",
    "            model.zero_grad()\n",
    "            # 2. 运行模型\n",
    "            model.eval()\n",
    "            with torch.no_grad():\n",
    "                pre = model(b_ids[0]).squeeze(0)\n",
    "                add =[]\n",
    "                for i in range(len(add_important[num1])):\n",
    "                    add.append(torch.argmax(pre[add_important[num1][i]]))\n",
    "                num1+=1\n",
    "            arr.append(add)\n",
    "        return arr"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "id": "327d145d",
   "metadata": {},
   "outputs": [],
   "source": [
    "speakers1 = ['inquire_fault','inquire_user','inform_service','inform_measure','confirm','other','other','other','other','hold_on']#客服标签\n",
    "speakers2 = ['inform_fault','inform_user','inquire_service','inquire_measure','confirm','other','request_change','request_cancel','request_close','other']#客户标签\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "id": "8b16c659",
   "metadata": {},
   "outputs": [],
   "source": [
    "def judge(pre,speaker):#判断是否需要根据角色调整\n",
    "    if pre in speakers1 and speaker =='客服' or pre in speakers2 and speaker =='客户':\n",
    "        return True\n",
    "    return False"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "id": "c774d4e5",
   "metadata": {},
   "outputs": [],
   "source": [
    "def correct(pre,speaker):#根据角色进行调整\n",
    "    if speaker =='客服':\n",
    "        for i in range(len(speakers2)):\n",
    "            if pre ==speakers2[i]:\n",
    "                return speakers1[i]\n",
    "    else:\n",
    "        for i in range(len(speakers1)):\n",
    "            if pre ==speakers1[i]:\n",
    "                return speakers2[i]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "id": "95c1e9ed",
   "metadata": {},
   "outputs": [],
   "source": [
    "def getchange(batch_ids,speakers):#将数字标签转换为字符串标签\n",
    "    batch = []\n",
    "    for i in range(len(batch_ids)):\n",
    "        key = batch_ids[i][0]\n",
    "        speaker = speakers[i]\n",
    "        pre = change_labels[key]\n",
    "        if  not judge(pre,speaker):\n",
    "            print(pre,speaker)\n",
    "            pre = correct(pre,speaker)\n",
    "        batch.append(pre)\n",
    "    return batch"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "id": "029cd75b",
   "metadata": {},
   "outputs": [],
   "source": [
    "def predictions(loader):#进行预测的功能块\n",
    "    right=0\n",
    "    pre_sum =[[0 for j in range(15)]for i in range(15)]\n",
    "    num=0\n",
    "    nn =0\n",
    "    Allpredictions = []\n",
    "    with torch.no_grad():\n",
    "        for batch_data in loader:#取出一个test的batch\n",
    "            nn+=1\n",
    "            b_speaker = batch_data['speaker']\n",
    "            test_ids = batch_data['ids'].to(device)\n",
    "            speakers = []\n",
    "            b_speaker = batch_data['speaker']\n",
    "            bb_speaker = []\n",
    "            for i in b_speaker:\n",
    "                i = list(i)[0]\n",
    "                if i =='客服':\n",
    "                    bb_speaker.append(i)\n",
    "                    speakers.append(-100)\n",
    "                else:\n",
    "                    bb_speaker.append(i)\n",
    "                    speakers.append(-200)\n",
    "            speakers = torch.tensor(speakers)\n",
    "            speakers = speakers.unsqueeze(1).unsqueeze(0).to(device)#获得这个batch的ids\n",
    "            prediction = getchange(model_FinetuneModel(test_ids[0],None,speakers,None),bb_speaker)#获得预测的数字标签并通过getchange转换为字符串标签\n",
    "            #prediction = getchange1(model(test_ids[0],None,speakers,None))\n",
    "            Allpredictions.append(prediction)#存储所有的预测获得的字符串标签\n",
    "            labels_test_batch = batch_data['labels']#获得所有的测试数据的正确标签\n",
    "#             print(nn,prediction)\n",
    "#             for i in range(len(labels_test_batch)):#进行比较并且计数\n",
    "#                 x = change_labels1[labels_test_batch[i][0]]\n",
    "#                 y = change_labels1[prediction[i]]\n",
    "#                 pre_sum[x][y]+=1\n",
    "#                 if labels_test_batch[i][0]==prediction[i]:\n",
    "#                     right+=1\n",
    "#                 num+=1\n",
    "    return right,num,Allpredictions,pre_sum"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "bert1",
   "language": "python",
   "name": "bert1"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.7.13"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
